<?php

namespace Agent\Controller;

use Think\Model;
use Home\Controller\SmsController;

class DrawingsController extends CommonController {
	public function __construct() {
		parent::__construct ();
		parent::__config ( array (
				'model_name' => 'Drawings' 
		) );
	}
	public function index() {
		$agent_info = session ( "ssc_agent" );
		parent::__config ( array (
				'where_in' => 'agent_id',
				'where_in_value' => $agent_info ['agent_id'] 
		) );
		$table = I ( 'table' );
		if (! $table == null && ! empty ( $table )) {
			parent::__config ( array (
					'display' => 'table',
					'display_parms' => array (
							'table' => 1 
					) 
			) );
		}
		;
		parent::index ();
	}
	function add() {
		$day = intval ( date ( 'd', time () ) );
		if ($day > 30) {
			$this->system_hint ( "请于1到10号申请提款!", 100 );
			$this->redirect ( 'index' );
		}
		$agent_info = session ( "ssc_agent" );
		$where ['agent_id'] = $agent_info ['agent_id'];
		$this->bean = D ( "Agent" )->where ( $where )->find ();
		parent::add ();
	}
	function insert() {
		$Smsverif = new SmsController ();
		$check_result = $Smsverif->checkCode ( I ( 'phone' ), I ( 'code' ) );
		if ($check_result) {
			$table = I ( 'table' );
			if (! $table == null && ! empty ( $table )) {
				parent::__config ( array (
						'display' => 'table',
						'display_parms' => array (
								'table' => 1 
						) 
				) );
			}
			;
			$agent_info = session ( "ssc_agent" );
			$agent_money = get_agent_money ( $agent_info ['agent_id'] );
			if ($_POST ['money'] > $agent_money) {
				$agent_info = session ( "ssc_agent" );
				$where ['agent_id'] = $agent_info ['agent_id'];
				$this->bean = D ( "Agent" )->where ( $where )->find ();
				$this->display ( "add" );
				die ();
			}
			$model = new Model ();
			$model->startTrans ();
			$data ['money'] = $_POST ['money'];
			$data ['bank_name'] = $_POST ['bank_name'];
			$data ['account'] = $_POST ['account'];
			$data ['account_username'] = $_POST ['account_username'];
			$data ['agent_id'] = $agent_info ['agent_id'];
			$data ['agent_code'] = $agent_info ['agent_code'];
			$data ['status'] = 1;
			$data ['created'] = time ();
			$data ['modfied'] = time ();
			$data ['deleted'] = 1;
			$add_result = D ( "Drawings" )->add ( $data );
			$data_agent ['money'] = $agent_money - $_POST ['money'];
			$data_agent ['agent_id'] = $agent_info ['agent_id'];
			$save_result = D ( "Agent" )->save ( $data_agent );
			if ($add_result && $save_result) {
				$model->commit ();
				$this->redirect ( 'index' );
			} else {
				$model->rollback ();
				$this->display ( "add" );
			}
		} else {
			$this->system_hint ( "验证码错误!", 100 );
		}
	}
	
	// function checkagentmoney(){
	// $money = I("money");
	// $agent_info = session("ssc_agent");
	// $agent_money = get_agent_money($agent_info['agent_id']);
	// if($money>$agent_money){
	// echo 1;
	// }else{
	// echo 2;
	// }
	// }
	function foreverdelete() {
		// parent::__config ( array (
		// 'display' => 'index?table=1' ,
		// ) );
		// parent::foreverdelete ();
	}
	function edit() {
		parent::__config ( array (
				'display' => 'index' 
		) );
		$data ['id'] = I ( 'id' );
		$data ['status'] = 1;
		parent::update ( $data );
		// parent::edit ();
	}
	function update() {
		parent::__config ( array (
				'display' => 'index' 
		) );
		$data ['id'] = I ( 'id' );
		$data ['status'] = 0;
		parent::update ( $data );
	}
	function logout() {
		session ( "ssc_agent", null );
		session ( 'agent', null );
		$this->redirect ( 'Home/Index/index' );
	}
	function bean() {
		parent::bean ();
	}
	function sort() {
		parent::sort ();
	}
	function excelImport() {
	}
	function excelOutput() {
	}
}